export const mixin = {
  data () {
    return {
      activatedFlag: false
    };
  },
  watch: {
    activatedFlag (n, o) {
      // console.log(n, o)
    }
  },
  mounted () {
    this.activatedFlag = true;
  },
  activated () {
    this.activatedFlag = true;
  },
  deactivated () {
    this.activatedFlag = false;
  }
}

import { goodsDetail, queryStock, batchQuerySpuStock, querySpu } from '@/api/box/workbench'
import { batchGetProductPromotionPrice } from '@/api/box/api'
export const skusStockMixin = {
  methods: {
    // 查询商详
    fetchGoodsDetail (productId, skuId) {
      const data = {
        requestData: {
          productId: productId,
          skuId: skuId
        }
      }
      return goodsDetail(data)
        .then(res => {
          if (res.code === 0) {
            return res.data
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log('查看商品信息:' + err)
        })
    },
    async getGoodSpu (item, productId, storeId, weid) {
      let goodsInfo = await this.fetchGoodsDetail(productId, '')
      let skusList = goodsInfo.skus

      if (skusList.length > 0) {
        let skusIds = skusList.map(e => e.id)
        let stockList = await this.getSkuStock(skusIds, storeId, weid)
        if (stockList) {
          // 处理库存
          for (let sku of skusList) {
            let result = stockList.filter(e => e.skuId == sku.id)
            if (result.length > 0) {
              sku.stock = result[0].stock > 0 ? result[0].stock : 0
            } else {
              sku.stock = 0
            }
          }
          let obj = this.getSkuOptions(skusList)

          console.log('skuOptionsObj', obj)

          item.skuOptions = obj.skuOptions
          item.colorList = obj.colorList
          this.$forceUpdate()

          console.log(item)
        }
      }
    },
    // 批量查询sku库存 for:POS+
    getSkuStock (skuIds, storeId, weid) {
      const data = {
        requestData: {
          storeId: storeId,
          skuIds: skuIds,
          weId: weid
        }
      }
      return queryStock(data)
        .then(res => {
          if (res.code === 0) {
            return res.data
          } else {
            this.$message.error(res.msg)
            return false
          }
        })
        .catch(err => {
          console.log('getSkuStock:' + err)
        })
    },
    getSkuOptions (skusList) {
      // 处理联级skuOptions
      let skuOptions = []
      let colorList = this.filterSizeColor(skusList, [], 'color')
      let sizeList = this.filterSizeColor(skusList, [], 'size')
      console.log(colorList, sizeList);

      let sizeOption = []
      for (let size of sizeList) {
        let children = {
          label: size.size_name,
          value: size.sizeno
        }
        sizeOption.push(children)
      }

      for (let option of colorList) {
        let item = { value: option.colorno, label: option.color_name, children: JSON.parse(JSON.stringify(sizeOption)), disabled: false }
        for (let size of item.children) {
          let result = skusList.filter(e => e.colorno == option.colorno && e.sizeno == size.value)
          if (result.length > 0) {
            size.skuId = result[0].id
            size.stock = result[0].stock
            size.disabled = result[0].stock == 0 ? true : false
            size.imgUrl = result[0].imgurl
          }
        }
        skuOptions.push(item)
      }
      // console.log(skuOptions)
      return { skuOptions, colorList }
    },
    // 筛选颜色尺码组
    filterSizeColor (data, list, type) {
      switch (type) {
        case 'color':
          const tempArr = list.map(item => item.colorno)
          const colors = data.map(item => {
            const color = {
              color_name: item.color_name,
              colorno: item.colorno
            }
            return color
          })
          for (var i = 0; i < colors.length; i++) {
            var temp = colors[i]
            if (tempArr.indexOf(temp.colorno) === -1) {
              tempArr.push(temp.colorno)
              list.push(temp)
            }
          }
          return list
        case 'size':
          const tempArr2 = list.map(item => item.sizeno)
          const sizes = data.map(item => {
            const size = {
              size_name: item.size_name,
              sizeno: item.sizeno
            }
            return size
          })
          for (var j = 0; j < sizes.length; j++) {
            var temp2 = sizes[j]
            if (tempArr2.indexOf(temp2.sizeno) === -1) {
              tempArr2.push(temp2.sizeno)
              list.push(temp2)
            }
          }
          list.sort(this.compare('sizeno'))
          return list
      }
    },
    // 尺码排序
    compare (property) {
      return function (a, b) {
        var value1 = a[property]
        var value2 = b[property]
        return value1 - value2
      }
    },
    // 批量查询优惠价
    getPromotionPrice (list) {
      let data = {
        requestData: {
          productList: list,
          unionId: this.unionId
        },
        // user_id: this.userInfo.id
      }
      return batchGetProductPromotionPrice(data)
        .then(res => {
          if (res.code == 0) {
            return res.data.productList
          } else {
            this.$message.error(res.msg)
            return []
          }
        })
        .catch(err => {
          console.log('ProductPromotionPrice:' + err)
        })
    },
    // 批量查spu库存
    getBatchQuerySpuStock (spus, storeId) {
      const data = {
        requestData: {
          productIds: spus,
          storeId: storeId
        },
        // user_id: this.userId
      }
      return batchQuerySpuStock(data)
        .then(res => {
          if (res.code == 0) {
            return res.data
          } else {
            this.$toast(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 批量查微盟spu库存
    getSpuStock (list) {
      let params = {
        requestData: {
          productIds: list,
          storeId: this.storeIdByLink,
          weId: this.weid
        }
      }
      return querySpu(params).then(res => {
        if (res.code == 0) {
          return res.data
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
  },

}